<template>
  <div class="app-container">
    <h1>菜品分类管理</h1>
    <h3 style="color: rgba(108,111,121,0.92)">{{`共有${this.data.length}种`}}</h3>
    <div style="margin: 20px 0;text-align: right">
      <el-button type="primary" size="mini" @click="dialogFormVisible=true">添加</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="data"
      element-loading-text="加载中"
      border
      stripe
      fit
      highlight-current-row
    >
      <el-table-column label="分类名" align="center" property="name">
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template scope="scope">
          <el-button size="small" type="success" @click="updateData(scope.row)">修改</el-button>
          <el-popconfirm style="margin-left: 35px"
                         @onConfirm="deleteData(scope.row.id)"
                         title="确定删除吗？">
            <el-button size="small" slot="reference" type="danger">删除</el-button>
          </el-popconfirm>

        </template>
      </el-table-column>

    </el-table>
    <el-dialog :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="formData" label-width="80px">
        <el-form-item label="分类名字">
          <el-col class="line" :span="23">
            <el-input v-model="formData.name" autocomplete="on"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false;formData={}">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import request from "@/utils/request";

  export default {
    name: "index",
    created() {
      this.initData()
    },
    data() {
      return {
        listLoading: true,
        data: [],
        dialogFormVisible: false,
        formData: {
          name: '',
          shopId: '',
        }
      }
    },
    methods: {
      initData() {
        console.log('this.$store.state.user.shop.id', this.$store.state.user.shop.id);
        this.listLoading = true
        request({
          url: '/classification',
          params: {
            shopId: this.$store.state.user.shop.id
          }
        }).then(v => {
          this.data = v.data
        }).finally(() => {
          this.listLoading = false
        })
      },
      deleteData(e) {
        console.log(e);
        request({
          url: '/classification',
          method: 'DELETE',
          params: {
            id: e
          }
        }).then(v => {
          this.$message({
            type: 'success',
            center: true,
            message: '删除成功！',
            duration: 1500
          });
        })
      },
      updateData(row) {
        this.dialogFormVisible = true
        this.formData = {...row}
      },

      save() {
        this.dialogFormVisible = false
        //这是修改
        if (this.formData.id) {
          delete this.formData.shopId
        } else {
          this.formData.shopId = this.$store.state.user.shop.id
        }
        request({
          url: '/classification',
          method: 'POST',
          data: {
            ...this.formData
          }
        }).then(v => {
          if (this.formData.id) {
            this.$message({
              type: 'success',
              center: true,
              message: '修改成功！',
              duration: 1500
            });
          } else {
            this.$message({
              type: 'success',
              center: true,
              message: '修改成功！',
              duration: 1500
            });
          }
          this.initData()
        })
      }
    }
  }
</script>

<style scoped>

</style>
